package org.rfs.ipodencoder;

import java.util.ArrayList;
import java.util.List;
import com.trolltech.qt.gui.*;

public class IPodEncoderMainView extends QMainWindow {

    IPodEncoderMain ui = new IPodEncoderMain();
    IPodEncoderController ctrl = new IPodEncoderController(this);
//    HashMap<String, HashMap<String, String>> mVideoInfo;

    public static void main(String[] args) {
        QApplication.initialize(args);
        
        IPodEncoderMainView IPodEncoderMainWindow = new IPodEncoderMainView();
        IPodEncoderMainWindow.show();
        
        QApplication.exec();
    }

    public IPodEncoderMainView() {
        ui.setupUi(this);
    }

    public IPodEncoderMainView(QWidget parent) {
        super(parent);
        ui.setupUi(this);
    }
    
    public void on_pushButton_add_clicked() {
    	List<String> fileList = QFileDialog.getOpenFileNames(this, 
				tr("Open Video File"), "", new QFileDialog.Filter(tr("Video Files (*.avi *.mkv *.mpg);;All Files (*.*)")));    	
    	ui.qFileListWidget.addItems(fileList);
    	ctrl.getInputFileInfo(fileList);
    }
    
    public void on_pushButton_del_clicked() {
    	if (ui.qFileListWidget.selectedItems().size() == 0) {
    		QMessageBox.information(this, "Selection required", "Select something in 'Input' to have it removed.");
    		return;
    	}
    	if (ctrl.removeInputFiles(ui.qFileListWidget.currentItem().text()) == null){
    		System.err.println("Internal error. " + ui.qFileListWidget.currentItem().text() + " could not be removed from mVideoInfo in IPodEncoderController...");
    	}
    	ui.qFileListWidget.takeItem(ui.qFileListWidget.currentRow());
    }
    
    public void on_pushButton_nfo_clicked() {
    	if (ui.qFileListWidget.selectedItems().size() == 0) {
    		QMessageBox.information(this, "Selection required", "Select something in 'Input' to get detailed information for.");
    		return;
    	}
    	String selectedFileName = ui.qFileListWidget.selectedItems().get(0).text();
    	IPodEncoderFileInfoView fInfo = new IPodEncoderFileInfoView(this, selectedFileName, ctrl.getInfoForFiles());
    	fInfo.applyEncodingSettings.connect(this, "getEncodingSettingsFromInfoDialog(String, String, String, String)");
    	fInfo.show();
    }
    
    public void getEncodingSettingsFromInfoDialog(String aBitrate, String vBitrate, String vFPS, String vScale) {
    	//System.out.println("connected " + aBitrate + " " + vBitrate + " " + vFPS +" " + vScale);
    	ui.lineEdit_abitrate.setText(aBitrate);
    	ui.lineEdit_vbitrate.setText(vBitrate);
    	ui.lineEdit_vframerate.setText(vFPS);
    	ui.lineEdit_vscale.setText(vScale);
    }
    
    public void on_pushButton_encode_clicked() {
    	ctrl.setEncodingOptions();

    	ArrayList<String> fileList = new ArrayList<String>();
    	for (int cnt=0; cnt<ui.qFileListWidget.count(); ++cnt) {
    		fileList.add(ui.qFileListWidget.item(cnt).text());
    	}
    	ctrl.encode(fileList, ui.comboBox_mode.currentIndex()+1); 
    	//TODO: future version could take a parameter which specifies the mode of encoding - so far we go with ffmpeg mp4 as default
    }
        
    public void on_qFileListWidget_dropped() {
    	ArrayList<String> fileList = new ArrayList<String>();
    	for (int cnt=0; cnt<ui.qFileListWidget.count(); ++cnt) {
    		fileList.add(ui.qFileListWidget.item(cnt).text());
    	}
    	ctrl.getInputFileInfo(fileList);
    }
}
